import socket

contacts = []

def handle_client(conn):
    while True:
        data = conn.recv(1024).decode()
        if not data:
            break
        
        command = data.split()
        if command[0] == "ADD":
            name, address, phone = command[1].split(',')
            contacts.append(f"{name}, {address}, {phone}")
            conn.sendall(b"Contact added successfully.")
        elif command[0] == "VIEW":
            all_contacts = "\n".join(contacts)
            conn.sendall(all_contacts.encode())
    
    conn.close()

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 5000))
server_socket.listen(5)

while True:
    client_conn, addr = server_socket.accept()
    handle_client(client_conn)